home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 52 / Mac Magazin and MacEasy Magazine CD - Issue 52.iso / Updates / Stata 5.0 Ado-files / ado.sea / newado / table.ado < prev    next >
Text File  |  1998-11-22  |  6KB  |  320 lines

  1. *! version 5.1.3  21may1998
  2. program define table
  3.     version 5.0
  4.  
  5.     local varlist "req ex max(3)"
  6.     local if "opt"
  7.     local in "opt"
  8.     local weight "fweight aweight pweight iweight"
  9.     local options "BY(string) COL CW Format(string) Name(string) REPLACE ROW SCol Contents(string) *"
  10.  
  11.     parse "`*'"
  12.     local sctotal "`scol'"
  13.     local coltota "`col'"
  14.     local rowtota "`row'"
  15.  
  16.     local stats "`content'"
  17.     local content
  18.  
  19.     if "`by'" != "" {
  20.         unabbrev `by'
  21.         local by "$S_1"
  22.         local byopt "by(`by')"
  23.     }
  24.  
  25.     parse "`varlist'", parse(" ")
  26.     local row "`1'"
  27.     local col "`2'"
  28.     local sc  "`3'"
  29.  
  30.     if "`coltota'"!="" & "`col'"=="" { 
  31.         local coltota 
  32.     }
  33.     if "`sctotal'"!="" & "`sc'"=="" {
  34.         local sctotal 
  35.     }
  36.  
  37.     if "`format'"=="" { 
  38.         local format "%9.0g"
  39.     }
  40.  
  41.     tempvar one touse
  42.  
  43.     if "`stats'"=="" {
  44.         local stats "freq"
  45.     }
  46.  
  47.     if "`replace'"!="" & "`name'"=="" {
  48.         local name "table"
  49.     }
  50.     local i 0
  51.     parse "`stats'", parse(" ")
  52.     while "`1'" != "" { 
  53.         local i = `i' + 1
  54.         if "`replace'"!="" {
  55.             local res "`name'`i'"
  56.         }
  57.         else    tempvar res
  58.         Parse "`weight'" `format' `res' `one' `*'
  59.         local clist "`clist' $S_1"
  60.         local cell  "`cell' `res'"
  61.         local vlist "`vlist' $S_2"
  62.         local flist "`flist' $S_4"
  63.         mac shift $S_3
  64.     }
  65.     if `i'>5 /* limit from tabdisp */ {
  66.         di in red "too many stats()"
  67.         exit 103
  68.     }
  69.     confirm new var `res'            /* in case replace    */
  70.  
  71.     quietly { 
  72.         if "`weight'" != "" {
  73.             tempvar wvar
  74.             gen double `wvar' `exp'
  75.             local wgt "[`weight'=`wvar']"
  76.         }
  77.         mark `touse' `wgt' `if' `in'
  78.         count if `touse'
  79.         if _result(1)==0 { 
  80.             noisily error 2000
  81.         }
  82.         preserve
  83.         keep if `touse'
  84.         drop `touse'
  85.         DropMis `varlist' `by'
  86.         if _N==0 { 
  87.             noisily error 2000
  88.         }
  89.         gen byte `one' = 1 
  90. /*
  91.         if "`weight'"=="aweight" {
  92.             summarize `wvar', mean
  93.             replace `wvar' = `wvar'/_result(3)
  94.             local wgt "[iweight=`wvar']"
  95.         }
  96. */
  97.         keep `varlist' `by' `vlist' `wvar'
  98.         if "`rowtota'"=="" & "`coltota'"=="" & "`sctotal'"=="" {
  99.             collapse `clist' `wgt', by(`varlist' `by') fast `cw'
  100.         }
  101.         else {
  102.             tempfile res orig
  103.             save "`orig'"
  104.             collapse `clist' `wgt', by(`varlist' `by') fast `cw'
  105.             save "`res'"
  106.             if "`rowtota'" != "" {
  107.                 AddRes "`res'" "`orig'" "`clist'" /*
  108.                 */ "`col' `sc' `by'" "`cw'" "`wgt'"
  109.             }
  110.             if "`coltota'"!= "" {
  111.                 AddRes "`res'" "`orig'" "`clist'" /*
  112.                 */ "`row' `sc' `by'" "`cw'" "`wgt'"
  113.                 if "`rowtota'" != "" {
  114.                     AddRes "`res'" "`orig'" "`clist'" /*
  115.                     */ "`sc' `by'" "`cw'" "`wgt'"
  116.                 }
  117.             }
  118.             if "`sctotal'"!="" {
  119.                 AddRes "`res'" "`orig'" "`clist'" /*
  120.                 */ "`row' `col' `by'" "`cw'" "`wgt'"
  121.                 if "`rowtota'" != "" {
  122.                     AddRes "`res'" "`orig'" "`clist'" /*
  123.                     */ "`col' `by'" "`cw'" "`wgt'"
  124.                 }
  125.                 if "`coltota'" != "" {
  126.                     AddRes "`res'" "`orig'" "`clist'" /*
  127.                     */ "`row' `by'" "`cw'" "`wgt'"
  128.                 }
  129.                 if "`rowtota'" != "" && "`coltota'"!="" {
  130.                     AddRes "`res'" "`orig'" "`clist'" /*
  131.                     */ "`by'" "`cw'" "`wgt'"
  132.                 }
  133.             }
  134.         }
  135.     }
  136.  
  137.                     /* set display formats        */
  138.     parse "`flist'", parse(" ") 
  139.     while "`1'" != "" { 
  140.         format `1' `2'
  141.         mac shift 2
  142.     }
  143.  
  144.  
  145.     if "`c'"=="" | "`replace'"!="" {
  146.         FixLabs "(count) `one'" `cell'
  147.     }
  148.  
  149.     if "`replace'"!="" {
  150.         mac drop S_FN S_FNDATE
  151.         restore, not
  152.     }
  153.     tabdisp `varlist', cell(`cell') `byopt' totals `options'
  154. end
  155.  
  156. program define AddRes /* resfn origfn clist by cw wgt */
  157.     local res   "`1'"
  158.     local orig  "`2'"
  159.     local clist "`3'"
  160.     local by    "`4'"
  161.     local cw    "`5'"
  162.     local wgt   "`6'"
  163.  
  164.     use "`orig'", clear
  165.     local n : word count `by'
  166.     if `n' {
  167.         collapse `clist' `wgt', by(`by') fast `cw'
  168.     }
  169.     else    collapse `clist' `wgt', fast `cw'
  170.  
  171.     append using "`res'"
  172.     save "`res'", replace
  173. end
  174.  
  175. program define Parse /* "weighttype" fmt newvar onevar stuff */
  176.     local weight "`1'"
  177.     local fmt "`2'"
  178.     local res "`3'"
  179.     local one "`4'"
  180.     mac shift 4
  181.     if "`1'"=="freq" {
  182.         global S_1 "(count) `res'=`one'"
  183.         global S_2 "`one'"
  184.         global S_3 1
  185.         if "`weight'"=="aweight" | "`weight'"=="iweight" | "`weight'"=="pweight" {
  186.             global S_4 "`res' `fmt'"
  187.         }
  188.         else    global S_4 "`res' %9.0g"
  189.         exit
  190.     }
  191.  
  192.     if "`2'"=="" { 
  193.         di in red "`1' invalid or requires argument"
  194.         exit 198 
  195.     }
  196.  
  197.                     /* synonyms    */
  198.     if lower("`1'")=="n" {
  199.         local 1 "count"
  200.     }
  201.     else if substr("median",1,max(3,length("`1'")))=="`1'" {
  202.         local 1 "median"
  203.     }
  204.     else if substr("mean",1,length("`1'"))=="`1'" {
  205.         local 1 "mean"
  206.     }
  207.  
  208.  
  209.     unabbrev `2', max(1)
  210.     local vn "$S_1"
  211.  
  212.     Valid `1' `vn' `fmt' `weight'
  213.     global S_4 "`res' $S_1"
  214.     global S_1 "(`1') `res'=`vn'"
  215.     global S_2 "`vn'"
  216.     global S_3 2
  217. end
  218.  
  219. program define Valid /* word fromvar dfltfmt weighttype */
  220.     local w "`1'"
  221.     local v "`2'"
  222.     local f "`3'"
  223.     local weight "`4'"
  224.  
  225.     if "`w'"=="sd" | "`w'"=="iqr" | "`w'"=="sum" | "`w'"=="rawsum" {
  226.         global S_1 "`f'"        /* meaning default format */
  227.         exit 
  228.     }
  229.  
  230.     if "`w'"=="count" {
  231.         if "`weight'"=="aweight" | "`weight'"=="iweight" | "`weight'"=="pweight" {
  232.             global S_1 "`f'"
  233.         }
  234.         else     global S_1 "%9.0g"    /* meaning as-is format */
  235.         exit
  236.     }
  237.  
  238.             /*
  239.                 remaining have default format or 
  240.                 variable's date format
  241.             */
  242.     local fmt : format `v'
  243.     if substr("`fmt'",1,2)=="%d" {
  244.         global S_1 "`fmt'"
  245.     }
  246.     else    global S_1 "`f'"
  247.  
  248.  
  249.     if "`w'"=="mean" | "`w'"=="median" { 
  250.         exit 
  251.     }
  252.     if "`1'"=="max" | "`1'"=="min" {
  253.         exit 
  254.     }
  255.     if substr("`1'",1,1)=="p" {
  256.         local n = substr("`1'",2,.)
  257.         capture confirm integer number `n'
  258.         if _rc==0 { 
  259.             if `n'>0 & `n'<100 { 
  260.                 exit
  261.             }
  262.         }
  263.     }
  264.     di in red "`1' invalid"
  265.     exit 198
  266. end
  267.  
  268. program define DropMis /* varnames */
  269.     while "`1'" != "" {
  270.         local t : type `1'
  271.         if substr("`t'",1,3)=="str" { 
  272.             drop if `1'=="" 
  273.         }
  274.         else    drop if `1'==.
  275.         mac shift 
  276.     }
  277. end
  278.  
  279. program define FixLabs /* lab varnames */
  280.     local     true "`1'"
  281.     mac shift
  282.     while "`1'" != "" {
  283.         local lab : variable label `1'
  284.         if "`lab'"=="`true'" {
  285.             label var `1' "Freq."
  286.         }
  287.         else {
  288.             FixLab2 `1' `lab'
  289.         }
  290.         mac shift
  291.     }
  292. end
  293.  
  294. program define FixLab2 /* label */
  295.     local var "`1'"
  296.     local wrd "`2'"
  297.     mac shift 2
  298.     if "`wrd'"=="(count)" {
  299.         label var `var' "N(`*')"
  300.         exit
  301.     }
  302.     if "`wrd'" == "(p" {
  303.         local wrd "`1'"
  304.         mac shift 
  305.         if "`wrd'"=="50)" {
  306.             label var `var' "med(`*')"
  307.         }
  308.         else {
  309.             local wrd = substr("`wrd'",1,length("`wrd'")-1)
  310.             label var `var' "p`wrd'(`*')"
  311.         }
  312.         exit
  313.     }
  314.     if substr("`wrd'",1,1)=="(" & substr("`wrd'",-1,1)==")" {
  315.         local wrd = substr("`wrd'",2,length("`wrd'")-2)
  316.         label var `var' "`wrd'(`*')"
  317.         exit
  318.     }
  319. end
  320.